<template>
  <c-form-dialog
    ref="form"
    :title="title"
    :visible.sync="innerVisible"
    :form-items="formItems"
    :form-data="formData"
    :table-name="tableName"
    :table-id="tableId"
    @dismiss="beforeClose"
    @success="handleSuccess"
  ></c-form-dialog>
</template>

<script>
import { dateFormat, sub } from '@vimi/utils-tool'
export default {
  components: {},
  props: {
    visible: Boolean,
    tableId: String,
    title: {
      type: String,
      default: '对账登记',
    },
    formData: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {
      innerVisible: false,
      tableName: 'credit_agency_capital_register',
      formItems: [
        {
          tag: 'date',
          itemAttrs: {
            label: '对账日期',
            rules: [{ required: true, message: '请选择对账日期', trigger: 'change' }],
          },
          attrs: { key: 'register_date', value: '', placeholder: '请选择对账日期', type: 'month' },
          getValue: value => {
            const date = new Date(value)
            return dateFormat(new Date(date.getFullYear(), date.getMonth() + 1, 0), 'yyyy-MM-dd')
          },
        },
        {
          tag: 'number',
          itemAttrs: {
            label: '初始金额',
            rules: [{ required: true, message: '请填写初始金额', trigger: 'change' }],
          },
          attrs: { key: 'init_funds', value: '', placeholder: '请填写初始金额', min: 0 },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'number',
          itemAttrs: {
            label: '当前金额',
            rules: [{ required: true, message: '请填写当前金额', trigger: 'input' }],
          },
          attrs: { key: 'current_funds', value: '', placeholder: '请填写当前金额', min: 0 },
          tagSlots: [{ name: 'append', slot: '万元' }],
        },
        {
          tag: 'number',
          itemAttrs: {
            label: '变动金额',
          },
          attrs: { key: 'diff_funds', value: '', placeholder: '请填写当前金额' },
          tagSlots: [{ name: 'append', slot: '万元' }],
          getAttrs: row => ({ value: sub(row.current_funds, row.init_funds), disabled: true }),
          ifShow: false,
        },
        {
          tag: 'textarea',
          itemAttrs: {
            label: '变动说明',
          },
          attrs: {
            key: 'diff_des',
            value: '',
            autosize: { minRows: 5 },
            placeholder: '请输入变动说明',
          },
        },
        {
          tag: 'input',
          attrs: { key: 'credit_agency_capital_id', value: '' },
          ifShow: false,
        },
      ],
    }
  },
  computed: {},
  watch: {
    visible(val) {
      this.innerVisible = val
    },
    formData: {
      handler: function () {
        this.$refs.form.setAttrs('init_funds', {
          value: this.formData.current_funds,
          disabled: true,
        })
        this.$refs.form.setAttrs('current_funds', { value: '' })
        if (this.formData.latest_register_date) {
          const date = new Date(this.formData.latest_register_date)
          const month = date.getMonth() + 1
          if (month > new Date().getMonth()) {
            this.$message.warning('当前月已对账')
            this.beforeClose()
          }
          this.$refs.form.setAttrs('register_date', {
            value: new Date(date.getFullYear(), month),
            disabled: true,
          })
        }
      },
      deep: true,
    },
  },
  created() {},
  mounted() {},
  methods: {
    beforeClose() {
      this.$emit('update:visible', false)
    },
    handleSuccess() {
      this.beforeClose()
      this.$emit('success', {})
    },
  },
}
</script>

<style lang="scss" scoped></style>
